package com.heimo.gogocheque.core;

public class ChineseDecimalFormat extends BasicConvertor {
	private String[] units = new String[] { "十", "百", "千", "萬", "十", "百", "千",
			"億" };
	private String[] numeric = new String[] { "零", "壹", "貳", "參", "肆", "伍",
			"陸", "柒", "捌", "玖" };

	public String convert(String temp) {
		if(temp.equalsIgnoreCase("0")){
			return numeric[0];
		}
		String res = "";
		for (int k = -1; temp.length() > 0; k++) {
			int j = Integer.parseInt(temp.substring(temp.length() - 1,
					temp.length()));
			String rtemp = numeric[j];
			if (j != 0 && k != -1 || k % 8 == 3 || k % 8 == 7) {
				rtemp += units[k % 8];
			}
			res = rtemp + res;
			temp = temp.substring(0, temp.length() - 1);
		}
		while (res.endsWith(numeric[0])) {
			res = res.substring(0, res.lastIndexOf(numeric[0]));
		}

		while (res.indexOf(numeric[0] + numeric[0]) != -1) {
			res = res.replaceAll(numeric[0] + numeric[0], numeric[0]);
		}
		for (int m = 1; m < units.length; m++) {
			res = res.replaceAll(numeric[0] + units[m], units[m]);
		}
		return res + "圓整";
	}
}
